package com.hcm.sk.exection;

import com.hcm.sk.vo.ResponseBean;
import com.hcm.sk.vo.ResponseBeanEnum;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;



/**
 * @description: GlobalExceptionHandler
 * @date: 2021/1/25 22:21
 * @author: hcm
 * @version: 1.0
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResponseBean ExceptionHnadler(Exception e){
        if(e instanceof GlobalException){
            GlobalException ex = (GlobalException) e;
            return ResponseBean.error(ex.getResponseBeanEnum());
        }else if(e instanceof BindException){
            BindException ex = (BindException) e;
            ResponseBean resp = ResponseBean.error(ResponseBeanEnum.BIND_ERROR);
            resp.setMessage("参数校验异常"+ ex.getBindingResult().getAllErrors().get(0).getDefaultMessage());
            return resp;
        }
        return ResponseBean.error(ResponseBeanEnum.ERROR);
    }
}
